#include "EWAUIWidgetVerticalScroll.hpp"

namespace Eyer
{
    int EWAUIWidgetVerticalScroll::OnDraw()
    {
        // 计算可滑动区域
        int scrollHeight = wh.height();
        int contentHeight = contentLayout->wh.height();

        progressBar->SetSize(contentHeight, scrollHeight);
        int marginTop = progressBar->GetMaginStart();

        offsetXY.y(marginTop);

        return 0;
    }

    int EWAUIWidgetVerticalScroll::Draw(int level)
    {
        std::vector<EyerSmartPtr<EWAUIGraph>> tempGraphList;
        // 绘制 Content
        contentLayout->OnMeasure(wh.w(), INT_MAX, level);
        contentLayout->Draw(level + 1);
        for(int i = 0; i < contentLayout->GetUIGraphSize(); i++){
            EyerSmartPtr<EWAUIGraph> g = contentLayout->GetUIGraph(i);
            // 重新计算位置, 转化为父对象的坐标系
            Vec2I tempXY = contentLayout->xy + offsetXY;
            g->Transform(tempXY, wh);
            if(g->IsValid()){
                tempGraphList.push_back(g);
            }
        }

        // 绘制子 Widget
        for(int i = 0; i < childWidget.size(); i++){
            const EyerSmartPtr<EWAUIWidget> & child = childWidget[i];
            // 绘制子内容
            child->Draw(level + 1);
            for(int i=0;i<child->GetUIGraphSize();i++){
                EyerSmartPtr<EWAUIGraph> g = child->GetUIGraph(i);
                // 重新计算位置, 转化为父对象的坐标系
                g->Transform(child->xy, wh);
                if(g->IsValid()) {
                    tempGraphList.push_back(g);
                }
            }
        }
        // 绘制本 Widget
        RemoveAllUIGraph();

        OnDraw();

        for(int i = 0; i < tempGraphList.size(); i++){
            graphList.push_back(tempGraphList[i]);
        }

        return 0;
    }

    bool EWAUIWidgetVerticalScroll::MousePressEvent     (const Vec2I & pos)
    {
        mouseStatus = EWAUIMouseStatus::PRESSED;
        isDrag = true;
        pressPos = pos;
        bool ret = OnMousePressEvent(pos);
        if(ret){
            // 向下传递
            for(int i = 0; i < childWidget.size(); i++) {
                const EyerSmartPtr<EWAUIWidget> & child = childWidget[i];
                Vec2I transPos = pos - child->xy;
                if(transPos.x() < 0){
                    continue;
                }
                if(transPos.y() < 0){
                    continue;
                }
                if(transPos.x() > child->wh.x()){
                    continue;
                }
                if(transPos.y() > child->wh.y()){
                    continue;
                }

                child->MousePressEvent(transPos);
            }

            Vec2I transPos = pos - (contentLayout->xy + offsetXY);
            if(transPos.x() < 0){
                return ret;
            }
            if(transPos.y() < 0){
                return ret;
            }
            if(transPos.x() > contentLayout->wh.x()){
                return ret;
            }
            if(transPos.y() > contentLayout->wh.y()){
                return ret;
            }
            contentLayout->MousePressEvent(transPos);
        }
        return ret;
    }

    bool EWAUIWidgetVerticalScroll::MouseReleaseEvent   (const Vec2I & pos)
    {
        if(mouseStatus != EWAUIMouseStatus::PRESSED){
            return false;
        }
        isDrag = false;
        mouseStatus = EWAUIMouseStatus::STAND_BY;
        bool ret = OnMouseReleaseEvent(pos);
        if(ret){
            // 向下传递
            for(int i = 0; i < childWidget.size(); i++) {
                const EyerSmartPtr<EWAUIWidget> & child = childWidget[i];
                Vec2I transPos = pos - child->xy;
                child->MouseReleaseEvent(transPos);
            }

            Vec2I transPos = pos - (contentLayout->xy + offsetXY);
            contentLayout->MouseReleaseEvent(transPos);
        }
        return ret;
    }

    bool EWAUIWidgetVerticalScroll::MouseMoveEvent      (const Vec2I & pos)
    {
        bool ret = OnMouseMoveEvent(pos);
        if(ret){
            // 向下传递
            for(int i = 0; i < childWidget.size(); i++) {
                const EyerSmartPtr<EWAUIWidget> & child = childWidget[i];
                Vec2I transPos = pos - child->xy;
                child->MouseMoveEvent(transPos);
            }

            Vec2I transPos = pos - (contentLayout->xy + offsetXY);
            contentLayout->MouseMoveEvent(transPos);
        }
        return ret;
    }

    bool EWAUIWidgetVerticalScroll::OnScrollEvent         (const double x, const double y)
    {
        // EyerLog("%f, %f\n", x, y);
        progressBar->MarginStartChange(progressBar->bar->marginTop - y);
        return true;
    }

    int EWAUIWidgetVerticalScroll::AddContentWidget(const EyerSmartPtr<EWAUIWidget> & widget)
    {
        contentLayout->AddWidget(widget);
        return 0;
    }

    EWAUIWidgetVerticalScroll::EWAUIWidgetVerticalScroll(EWAUILayoutParams _width, EWAUILayoutParams _height)
        : EWAUIWidget(_width, _height)
        , contentLayout(new EWAUILayout(EWAUILayoutType::LinearLayoutVertical, EWAUILayoutParams::MATCH_PARENT, EWAUILayoutParams::WRAP_CONTENT))
        , progressBar(new EWAUIProgressBar(BarType::VERTICAL, EWAUILayoutParams::WRAP_CONTENT, EWAUILayoutParams::MATCH_PARENT))
    {
        SetLayoutType(EWAUILayoutType::RetativeLayout);
        contentLayout->SetBGColor(EyerColor::YELLOW);
        progressBar->alignParentRight = true;
        AddWidget(progressBar);
        offsetXY.SetX(0);
        offsetXY.SetY(0);
    }

    EWAUIWidgetVerticalScroll::~EWAUIWidgetVerticalScroll()
    {

    }
}